package com.heima.datasync.service.Impl;

import com.heima.common.exception.CustException;
import com.heima.datasync.mapper.ApArticleMapper;
import com.heima.datasync.service.EsDataService;

import com.heima.es.service.EsService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.vos.SearchArticleVO;
import org.springframework.beans.BeanUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EsDataServiceImpl implements EsDataService {
    public static final String ARTICLE_INDEX_NAME = "app_info_article";
    @Autowired
    private EsService<SearchArticleVO> esService;
    @Autowired
    ApArticleMapper apArticleMapper;

    @Override
    public ResponseResult dataInit() {
        //判断索引库是否存在
        boolean isHas = esService.existIndex(ARTICLE_INDEX_NAME);
        //批量导入数据
        if (!isHas) {
            esService.createIndex("{\n" +
                    "    \"mappings\":{\n" +
                    "        \"properties\":{\n" +
                    "            \"id\":{\n" +
                    "                \"type\":\"long\"\n" +
                    "            },\n" +
                    "            \"publishTime\":{\n" +
                    "                \"type\":\"date\"\n" +
                    "            },\n" +
                    "            \"layout\":{\n" +
                    "                \"type\":\"integer\"\n" +
                    "            },\n" +
                    "            \"images\":{\n" +
                    "                \"type\":\"keyword\",\n" +
                    "                \"index\": false\n" +
                    "            },\n" +
                    "           \"staticUrl\":{\n" +
                    "                \"type\":\"keyword\",\n" +
                    "                \"index\": false\n" +
                    "            },\n" +
                    "            \"authorId\": {\n" +
                    "          \t\t\"type\": \"long\"\n" +
                    "       \t\t},\n" +
                    "          \"title\":{\n" +
                    "            \"type\":\"text\",\n" +
                    "            \"analyzer\":\"ik_smart\"\n" +
                    "          }\n" +
                    "        }\n" +
                    "    }\n" +
                    "}", ARTICLE_INDEX_NAME);
        }

        //分页查询第一页数据
        List<ApArticle> apArticles = apArticleMapper.findAllArticles();
        if (apArticles==null||apArticles.size()==0){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"数据库信息不存在");
        }
        //遍历页码批量插入到索引库
       List<SearchArticleVO> list = apArticles .stream().map(apArticle->{
            SearchArticleVO articleVO = new SearchArticleVO();
            BeanUtils.copyProperties(apArticle,articleVO);
            return articleVO;
        }).collect(Collectors.toList());
       /* for (ApArticle apArticle : apArticles) {
           //封装数据
            SearchArticleVO articleVO = new SearchArticleVO();
            BeanUtils.copyProperties(apArticle,articleVO);
            list.add(articleVO);
        }*/
        //将文章集合 批量导入到es索引库中
        esService.saveBatch(list,ARTICLE_INDEX_NAME);
        return ResponseResult.okResult();
    }
}
